package com.qingke.beSuper2;

import com.qingke.beSuper2.command.CommandFactory;
import com.qingke.beSuper2.command.CommandFactory.CommandCode;
import com.qingke.beSuper2.command.Command;
import com.qingke.beSuper2.pojo.Player;
import com.qingke.beSuper2.utils.QingkeConsole;

public class BeSuperApplication {

	private Player playerProfile;
	
	private static BeSuperApplication instance;
	
	private BeSuperApplication() {
	}
	
	public static synchronized BeSuperApplication getInstance() {
		if (instance == null) {
			instance = new BeSuperApplication();
		}
		return instance;
	}
	
	public static void main(String[] args) {
		
		QingkeConsole.println("Welcome to play this game, you can use the \"HELP\" for command usage.");
		
		while(true) {
			String cmd = QingkeConsole.askUserInput("cmd > ");
			CommandCode cmdCode = null;
			
			try {
				cmdCode = CommandCode.valueOf(cmd.toUpperCase());
			} catch (Exception e) {
				QingkeConsole.println("Please enter a valid command");
				continue;
			}
			
			Player player = BeSuperApplication.getInstance().getPlayerProfile();
			CommandFactory commandFactory = 
					CommandFactory.buildFactory(player);
			Command command = commandFactory.buildCommand(cmdCode);
			if (command != null) {
				command.execute();
			}
		}
		
	}

	public Player getPlayerProfile() {
		return playerProfile;
	}
	
	public void setPlayerProfile(Player player) {
		this.playerProfile = player;
	}
	
}
